package library.logger;


public class WordUtils {
    private static final String SP_CRLF = System.getProperty("line.separator");
    private static final String DEFAULT_CRLF = "\n";
    public static final String CRLF = SP_CRLF != null ? (SP_CRLF.contains(DEFAULT_CRLF) ? DEFAULT_CRLF : SP_CRLF) : DEFAULT_CRLF;

    public static String wrap(final String str, final int wrapLength) {
        String[] trace = str.split(CRLF);
        StringBuilder sb = new StringBuilder(str.length());
        for (int i = 0; i < trace.length; i++) {
            sb.append(wrap(trace[i], wrapLength, CRLF));
            if (i < trace.length - 1)
                sb.append(CRLF);
        }
        return sb.toString();
    }

    private static String wrap(final String str, final int wrapLength, final String CRLF) {
        if (str == null && str.length() < 1 && wrapLength < 1)
            return null;
        final int str_length = str.length();
        final int str_size = str.replaceAll("[^\\x00-\\xFF]", "cn").getBytes().length;
        if (str_size <= wrapLength)
            return str;
        StringBuilder sb = new StringBuilder(str_length);
        int line_start = 0;
        int line_split = 0;
        int line_offset = 0;
        int byte_size = 0;
        int byte_split = 0;
        int wrap_offset = wrapLength;
        for (line_offset = 0; line_offset < str_length; ) {
            char c = str.charAt(line_offset);
            if (isASCII(c)) {//单字节
                byte_size += 1;
                if (isEnglish(c)) {//英文字母

                } else {//其他符号
                    line_split = line_offset;
                    byte_split = byte_size;
                }
            } else {//双字节
                byte_size += 2;
                line_split = line_offset;
                byte_split = byte_size;
            }
            if (byte_size > wrap_offset) {
                if (line_split > 0) {
                    if (!isASCII(str.charAt(line_split))) {
                        line_offset = line_split;
                        byte_size = byte_split;
                    } else {
                        line_offset = line_split + 1;
                        byte_size = byte_split + 1;
                    }
                }
                String line = str.substring(line_start, line_offset);
                sb.append(line);
                sb.append(CRLF);
                if (wrap_offset >= str_size) {
                    break;
                }
                wrap_offset = byte_size + wrapLength - ((byte_size > (wrap_offset + 1)) ? 1 : 0);
                line_start = line_offset;
                line_split = 0;
            } else {
                line_offset++;
            }
        }
        sb.append(str.substring(line_start));
        return sb.toString();
    }

    private static boolean isASCII(char c) {
        return c >= 0x00 && c < 0xFF;
    }

    private static boolean isEnglish(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }

    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }
}
